package test.socket;

import java.net.InetSocketAddress;

import hyl.base.net.iot.ABsrv;
import hyl.base.net.iot.Conf;
import hyl.base.net.iot.Csrv;

import hyl.base.net.iot.Netpg3;
import hyl.core.MyDate;
import hyl.core.MyFun;

public class testiotsrv {
	InetSocketAddress addrc=new InetSocketAddress("127.0.0.1",9090);
	InetSocketAddress addrb=new InetSocketAddress("127.0.0.1",9080);
	InetSocketAddress addra=new InetSocketAddress("127.0.0.1",9070);

	ABsrv asrv;
	public void startA(InetSocketAddress address) {		
		//启动c服务器
		asrv = new ABsrv ("1001",120000,address,addra);

		asrv.h上行后置器=(reader, bte)->{
			Netpg3 npNetpg3 = new Netpg3();
			byte[] sy = npNetpg3.deserial(bte);
			System.out.println("测试A:收到B端的反馈 "+ npNetpg3);
			return false;
		};
		asrv.start();			
		//启动d终端 连接c
	}
	
	
	ABsrv bsrv;
	public void startB(InetSocketAddress address) {		
		//启动c服务器
		bsrv = new ABsrv ("2001",120000,address,addrb);
		bsrv.h上行后置器=(reader, bte)->{
			Netpg3 npNetpg3 = new Netpg3();
			byte[] sy = npNetpg3.deserial(bte);
			System.out.println("测试B:收到C端的反馈 "+ npNetpg3);
			return false;
		};
		bsrv.start();			
		//启动d终端 连接c
	}
	
	Csrv csrv;
	public void startC(InetSocketAddress address) {		
		//启动c服务器
		csrv = new Csrv ("3001",120000,address,addrc);
		csrv.h上行后置器=(reader, bte)->{
			System.out.println("测试C:收到D端的反馈 "+new String(bte,Conf.D编码));
			return false;
		};
		csrv.start();		

	}
	

	public void a的发送(int i) {
		Netpg3 pg3=new Netpg3();
		pg3.ii路径=new int[] {4001,3001,2001,1001};
		pg3.b指令1=Netpg3.CMD_文本;
		pg3.s请求="open="+i+";";
		asrv.sendToBC(pg3);
	}
	public void b的发送(int i) {
		Netpg3 pg3=new Netpg3();
		pg3.ii路径=new int[] {4001,3001,2001};
		pg3.b指令1=Netpg3.CMD_文本;
		pg3.s请求="open="+i+";";
		bsrv.sendToBC(pg3);
	}
	public void c的发送(int i) {
		Netpg3 pg3=new Netpg3();
		pg3.ii路径=new int[] {4001,3001};
		pg3.b指令1=Netpg3.CMD_文本;
		pg3.s请求="open="+i+";";
		csrv.sendToD(pg3);
	}
	
	
	public static void main(String[] args) {
		testiotsrv p3 =new testiotsrv();

	//	p3.ttdc5000();
		//p3.ttdcb5000();
		p3.ttdcba5000();
	}

	public void ttdc5000() {
		//启动a服务器
		//启动b服务器 连接a
		//启动c服务器 连接b
		//启动d终端 连接 c
		//通过a发送消息给 d
		//通过d发送消息给a
		startC(null);		
		MyDate.sleep(3000);
		//通过c发送消息给 d	
		for(int i=0;i<5000;i++) {	
		
			c的发送(i) ;
		
		}
		System.out.println("---end");
	}
	public void ttdcb5000() {
		//启动a服务器
		//启动b服务器 连接a
		//启动c服务器 连接b
		//启动d终端 连接 c
		//通过a发送消息给 d
		//通过d发送消息给a
		startB(null);
		startC(addrb);		
		MyDate.sleep(3000);
		long st=MyFun.getMs();
		//通过c发送消息给 d	
		for(int i=0;i<5000;i++) {			
			b的发送(i) ;
		}
		 st=MyFun.getMs()-st;
		System.out.println("---end"+st);
	}
	public void ttdcba5000() {
		//启动a服务器
		//启动b服务器 连接a
		//启动c服务器 连接b
		//启动d终端 连接 c
		//通过a发送消息给 d
		//通过d发送消息给a
	
		startA(null);
		startB(addra);
		startC(addrb);		
		MyDate.sleep(3000);
		//通过c发送消息给 d	
		long st=MyFun.getMs();
		for(int i=0;i<5000;i++) {
			//System.out.println("发送"+i);
			a的发送(i) ;
			//MyDate.sleep(1000);
		}
		 st=MyFun.getMs()-st;
			System.out.println("---end"+st);
	}
}
